<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class LunboController extends Controller
{
    //列表页
    public function index()
    {
    	$title = '轮播图管理';
    	$data = DB::table('lunbo') -> first();
    	// dd($data);

    	return view('admin.lunbo.index',['title' => $title,'data' => $data]);
    }

    //执行更行页
    public function update(Request $request)
    {
    	$data = $request -> except('_token','id','oldpica','oldpicb','oldpicc','oldpicd','oldpice');
    	$id = $request -> input('id');

    	//获取老图片地址
        $oldapath = '.'.$request -> input('oldpica');
        $oldbpath = '.'.$request -> input('oldpicb');
        $oldcpath = '.'.$request -> input('oldpicc');
        $olddpath = '.'.$request -> input('oldpicd');
        $oldepath = '.'.$request -> input('oldpice');
        // dd($oldapath);
        //处理pica
        if($request -> hasFile('pica'))
        {
            if($request -> file('pica') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('pica') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('pica') -> move($path,$fileName);

                $data['pica'] = trim($path.'/'.$fileName,'.');
                // dd($data);
               
                if(file_exists($oldapath))
                {
                    unlink($oldapath);
                }
                
            }
        }

        //处理picb
        if($request -> hasFile('picb'))
        {
            if($request -> file('picb') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('picb') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('picb') -> move($path,$fileName);

                $data['picb'] = trim($path.'/'.$fileName,'.');
                // dd($data);
               
                if(file_exists($oldbpath))
                {
                    unlink($oldbpath);
                }
                
            }
        }

        //处理picc
        if($request -> hasFile('picc'))
        {
            if($request -> file('picc') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('picc') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('picc') -> move($path,$fileName);

                $data['picc'] = trim($path.'/'.$fileName,'.');
                // dd($data);
               
                if(file_exists($oldcpath))
                {
                    unlink($oldcpath);
                }
                
            }
        }

        //处理picd
        if($request -> hasFile('picd'))
        {
            if($request -> file('picd') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('picd') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('picd') -> move($path,$fileName);

                $data['picd'] = trim($path.'/'.$fileName,'.');
                // dd($data);
               
                if(file_exists($olddpath))
                {
                    unlink($olddpath);
                }
                
            }
        }

        //处理pice
        if($request -> hasFile('pice'))
        {
            if($request -> file('pice') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('pice') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('pice') -> move($path,$fileName);

                $data['pice'] = trim($path.'/'.$fileName,'.');
                // dd($data);
               
                if(file_exists($oldepath))
                {
                    unlink($oldepath);
                }
                
            }
        }

        //执行添加数据
        $res = DB::table('lunbo') -> where('id',$id) -> update($data);
      
        //判断
        if($res)
        {
            return redirect('/admin/lunbo/index') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }
    }
}

